package com.example.webservice_client;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


public class WebServiceClient extends Activity {
    private final String NAMESPACE = "http://www.w3schools.com/webservices/";
    private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
    private final String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
    private final String METHOD_NAME = "CelsiusToFahrenheit";

    private String TAG = "PGGURU";
    private static String celcius;
    private static String fahren;
    Button b;
    TextView tv;
    EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et = (EditText) findViewById(R.id.editText1);
        tv = (TextView) findViewById(R.id.tv_result);
        b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(et.getText().length() != 0 && et.getText().toString() != "") {
                    celcius = et.getText().toString();
                    AsyncCallWS task = new AsyncCallWS();
                    task.execute();
                } else {
                    tv.setText("Please enter Celcius");
                }
            }
        });
    }


    private class AsyncCallWS extends AsyncTask<String, Void, Void> {
        @Override
        protected Void doInBackground(String... params) {
            Log.i(TAG, "doInBackground");
            getFahrenheit(celcius);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.i(TAG, "onPostExecute");
            tv.setText(fahren + "° F");
        }

        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
            tv.setText("Calculating...");
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            Log.i(TAG, "onProgressUpdate");
        }

    }

    public void getFahrenheit(String celsius) {
        //Create request
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        //Property which holds input parameters
        PropertyInfo celsiusPI = new PropertyInfo();
        celsiusPI.setName("Celsius");//Set Name
        celsiusPI.setValue(celsius);//Set Value
        celsiusPI.setType(double.class);//Set dataType
        request.addProperty(celsiusPI);//Add the property to request object

        //Create envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);//Set output SOAP object
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);//Create HTTP call object

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);//Invole web service
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();//Get the response
            // soap 可以将通讯结果的xml 转换成对象
            // SoapObject soapObject = (SoapObject) envelope.getResponse();
            //String result = "产品名称：" + soapObject.getProperty("name") + "\n";
            //result += "产品数量：" + soapObject.getProperty("productNumber") + "\n";
            //result += "产品价格：" + soapObject.getProperty("price");
            fahren = response.toString();//Assign it to fahren static variable
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
